<?php
namespace app\admin\controller;

class Option extends Base
{
    public function __construct()
    {
        parent::__construct();
        $this->isLogin();
    }

    public function add()
    {
        $name = I('post.name');
        if(empty($name))
            return $this->error('配置组名不能为空', U('Option/index'));
        $optionModel=D("Option");
        $addOption = $optionModel->addOption($name);
        if($addOption){
			 manage_log_success();
            return $this->success('修改成功', U('Option/index'));
        }else{
			 manage_log_error();
            return $this->error('修改失败', U('Option/index'));
        }			
    }

    public function index()
    {
        $optionModel=D("Option");
        $list = $optionModel->getList();
        $formCon = new Form();
        foreach($list as &$vo){
            $vo['form'] = $formCon->createForm($vo['item'], $vo['value'], $vo['id']);
        }
        unset($vo);
        $this->assign('list', $list);
        return $this->fetch();
    }
	
    public function edit()
    {
	    if(IS_POST){
            $id = I('get.id');
            $setValue = D("Option")->setValue($id);
            if($setValue){
				 manage_log_success();
			    return $this->success('修改成功', U('Option/index'));
		    }else{
				 manage_log_error();
			    return $this->error('修改失败', U('Option/index'));
		    }			
		} 
    }

    public function del()
    {
        $reconfirm = $this->reconfirm('删除后无法恢复，是否确认删除？');
        if(!$reconfirm)
            return $this->error('取消删除', U('Option/index'));
        $id = I('get.id');
        $delOption = D("Option")->delOption($id);
        if($delOption){
			manage_log_success();
            return $this->success('删除成功', U('Option/index'));
        }else{
			manage_log_error();
            return $this->error('删除失败', U('Option/index'));
        }			
    }
	
    public function addItem()
    {
        $id = I('get.id');
        $addItem = D("Option")->addItem($id);
        if($addItem){
			manage_log_success();
            return $this->success('添加成功', U('Option/index'));
        }else{
			manage_log_error();
            return $this->error('添加失败', U('Option/index'));
        }
    }

    public function delItem()
    {
        $id  = I('get.id');
        $tag = I('get.tag');
        $delItem = D("Option")->delItem($id, $tag);
        if($delItem){

			manage_log_success();
            return $this->success('删除成功', U('Option/index'));
        }else{
			manage_log_error();
            return $this->error('删除失败', U('Option/index'));
        }
    }
}

